﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using TWS.Client;

public partial class admin_ChiTietBanner : BaseAdminPage
{
    protected void btnOK_OnClick(object sender, EventArgs e)
    {
        //Page.Validate();
        //if (!Page.IsValid) return;
        //BannerQuangCaoHelper spHP = new BannerQuangCaoHelper();
        //BannerQuangCaoDataSet spDS = new BannerQuangCaoDataSet();
        //BannerQuangCaoDataSet.BannerQuangCaoRow BannerQuangCaomoi = spDS.BannerQuangCao.NewBannerQuangCaoRow();
        //BannerQuangCaomoi.TenBannerQuangCao = txtTenBannerQuangCao.Text;
        //BannerQuangCaomoi.LoaiBannerQuangCao = Convert.ToInt32(ddlLoaiCon.SelectedValue);
        //BannerQuangCaomoi.HangSanXuat = txtHangSanXuat.Text;
        //BannerQuangCaomoi.ChungLoai = txtChungLoai.Text;
        //BannerQuangCaomoi.Gia = Convert.ToInt32(txtGia.Text);
        //////UploadImage();
        //if (txtHinhAnh.Text == string.Empty)
        //{
        //    txtHinhAnh.Text = "noimage.jpg";
        //}
        //BannerQuangCaomoi.HinhAnh = txtHinhAnh.Text;
        //BannerQuangCaomoi.BannerQuangCaoMoi = ckBannerQuangCaoMoi.Checked;
        //BannerQuangCaomoi.ThuTu = Convert.ToInt32(txtThuTu.Text);
        //BannerQuangCaomoi.NgayDang = DateTime.Now;

        //BannerQuangCaomoi.TongQuan = txtTongQuan.Text;
        //BannerQuangCaomoi.ThongSoKyThuat = txtThongSo.Text;
        //BannerQuangCaomoi.Album = txtAlbum.Text;
        //spDS.BannerQuangCao.AddBannerQuangCaoRow(BannerQuangCaomoi);
        //spHP.SaveBannerQuangCao(spDS);
        //RedirectPage(Request.RawUrl, true);
        SaveData();
    }

    private BannerQuangCaoDataSet m_BannerQuangCaoDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
            LoadData(Convert.ToInt32(ddlLoaiCha.SelectedValue));
        }
    }

    private void LoadData(int loaisanphamID)
    {

        BannerQuangCaoHelper helper = new BannerQuangCaoHelper();
        BannerQuangCaoDataSet tempDs = helper.LoadBannerQuangCaoTheoLoaiSanPham(loaisanphamID);

        if (tempDs.BannerQuangCao.Rows.Count > 0)
        {


            BannerQuangCaoDataSet.BannerQuangCaoRow danhMuc = tempDs.BannerQuangCao[0];
            m_BannerQuangCaoDataSet = new BannerQuangCaoDataSet();
            m_BannerQuangCaoDataSet.BannerQuangCao.ImportRow(danhMuc);

            Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"] = m_BannerQuangCaoDataSet;


            if (!danhMuc.IsLoaiSanPhamNull()) ddlLoaiCha.SelectedValue = danhMuc.LoaiSanPham.ToString();
            txtTenSanPham.Text = danhMuc.TenCongTy;
            lbTenSanPham.Text = ddlLoaiCha.SelectedItem.Text;
            if (!danhMuc.IsLinkNull()) txtLink.Text = danhMuc.Link;

            //if (!BaiVietHay.IsHinhAnhNull()) {
            if (!danhMuc.IsHinhAnhNull()) txtHinhAnh.Text = danhMuc.HinhAnh;
            string saveLocation = MyFilePathHelper.GetImageFolderURL(this);
            this.imageDemons.Src = saveLocation + txtHinhAnh.Text;
            //}
        }
        else
        {
            m_BannerQuangCaoDataSet = new BannerQuangCaoDataSet();

            BannerQuangCaoDataSet.BannerQuangCaoRow danhMuc = m_BannerQuangCaoDataSet.BannerQuangCao.NewBannerQuangCaoRow();
            danhMuc.ID = -1;
            danhMuc.HinhAnh = "noimage.jpg";
            danhMuc.Link = "";
            danhMuc.TenCongTy = "";

            m_BannerQuangCaoDataSet.BannerQuangCao.AddBannerQuangCaoRow(danhMuc);
            Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"] = m_BannerQuangCaoDataSet;


            txtTenSanPham.Text = danhMuc.TenCongTy;
            lbTenSanPham.Text = ddlLoaiCha.SelectedItem.Text;
            if (!danhMuc.IsLinkNull()) txtLink.Text = danhMuc.Link;

            //if (!BaiVietHay.IsHinhAnhNull()) {
            if (!danhMuc.IsHinhAnhNull()) txtHinhAnh.Text = danhMuc.HinhAnh;
            string saveLocation = MyFilePathHelper.GetImageFolderURL(this);
            this.imageDemons.Src = saveLocation + txtHinhAnh.Text;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.RedirectLastVisitedPage();
    }

    private void SaveData()
    {
        try
        {
            string mode = Request.QueryString["mode"];
            m_BannerQuangCaoDataSet = (BannerQuangCaoDataSet)Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"];
            //SetViewMode(canEdit);
            BannerQuangCaoDataSet.BannerQuangCaoRow danhMuc = m_BannerQuangCaoDataSet.BannerQuangCao[0];

            danhMuc.TenCongTy = txtTenSanPham.Text;
            danhMuc.Link = txtLink.Text;
            danhMuc.LoaiSanPham = Convert.ToInt32(ddlLoaiCha.SelectedValue);
            UploadImage();
            if (txtHinhAnh.Text == string.Empty)
            {
                txtHinhAnh.Text = "noimage.jpg";
            }
            danhMuc.HinhAnh = txtHinhAnh.Text;
            

            BannerQuangCaoHelper helper = new BannerQuangCaoHelper();
            helper.SaveBannerQuangCao(m_BannerQuangCaoDataSet);
        }
        catch (Exception ex)
        {
            //DisplayMesseage(ex.Message);
            this.RedirectPage("pgError.aspx", false);
        }
        this.RedirectPage("ChiTietBanner.aspx", false);
    }

    private void UploadImage()
    {
        if (chkDeleteFile.Checked && txtHinhAnh.Text != string.Empty)
        {
            //delete old image
            try
            {
                string saveLocation = MyFilePathHelper.GetImageFolderURL_Saving(this);
                MyFilePathHelper.DeleteResource(saveLocation, txtHinhAnh.Text);
            }
            catch (Exception)
            {
            }
            finally
            {
                txtHinhAnh.Text = "noimage.jpg";
            }
            //postpone new image
        }
        //save new image
        if (this.inputFile.PostedFile != null && this.inputFile.PostedFile.ContentLength > 0)
        {
            string fn = System.IO.Path.GetFileName(inputFile.PostedFile.FileName);
            string path = MyFilePathHelper.GetImageFolderURL_Saving(this);
            fn = MyFilePathHelper.AppendTimeStamp(fn);
            string saveLocation = path + fn;

            inputFile.PostedFile.SaveAs(saveLocation);
            FileStream fs = File.Open(saveLocation, FileMode.Open, FileAccess.Read);
            byte[] inByte = new byte[fs.Length];
            fs.Read(inByte, 0, inByte.Length);
            fs.Close();
            MyFilePathHelper.DeleteResource(path, fn);
            ImagesConvertHelper iconvert = new ImagesConvertHelper();
            string filename;
            if (iconvert.ConvertImage(saveLocation, inByte, 6, out filename) == false)
                txtHinhAnh.Text = string.Empty;
            else
            {
                txtHinhAnh.Text = fn + ".JPG";
            }
        }
    }

    protected void ddlLoaiCha_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(ddlLoaiCha.SelectedValue);
        LoadData(id);
    }

    private void LoadData()
    {
        LoaiSanPhamDataSet lspDS = new LoaiSanPhamHelper().LoadLoaiSanPhamCha();
        ddlLoaiCha.DataSource = lspDS.LoaiSanPham;
        ddlLoaiCha.DataTextField = "TenLoaiSanPham";
        ddlLoaiCha.DataValueField = "LoaiSanPhamID";
        ddlLoaiCha.DataBind();


    }
}